<template>
	<view>
			 <view >  
	<view>
	<uni-section :title="titletype" type="line"></uni-section>

				<ad  unit-id="{{_feedsad}}" type="feeds"></ad>
		<view class="uni-padding-wrap">
			<view class="uni-title uni-common-mt uni-common-pl">{{questionT}}</view>
	<view>
			
		<view class="example-body message">
			<button class="button popup__success" @click="looke()"><text class="button-text">查看答案</text></button>
			<button class="button popup__error" @click="next()" ><text class="button-text">下一个</text></button>
	
		</view>
        	<ad  unit-id="{{_banner}}"></ad>

		</view>
</view>
			</view>
			 </view>
			 

				<!-- 消息提示 -->
		<uni-popup id="popupMessage" ref="popupMessage" type="message" >
			<uni-popup-message :type="msgType" :message="message" :duration="2000"></uni-popup-message>
		</uni-popup>
				<!-- 对话框 -->
		<uni-popup id="popupDialog" ref="popupDialog" type="dialog">
			<uni-popup-dialog :type="msgType" :title="title" :content="message" :before-close="true" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		</view>    
</template>
<script>
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	//const requestUrl ='https://asxsyd92.com/api/question/getquestion?tab=&name='
	const duration = 2000
	export default {
		components: {
	uniPopupMessage,
			uniPopupDialog
        },
		data() {
			return {
            
                _feedsad:"",
				_banner:"",
                _shiping:"",
                _chapingad:'',
				question:[],
				questionT:"",
				titletype:"趣味答题",
				current: 0,
					typeid:"",
			
				questionItem:[],
          		type: 'top',
				msgType: 'success',
				message: '这是一条成功消息提示',
				value: '默认输入的内容',
				title:'温馨提示',
			 videoAd : null
			}

		},
		onLoad(e){
	// 页面打开自动打开对话框
			  this.feedsad.sort(() => Math.random() - 0.5);
			 this. _feedsad= this.feedsad[0];
			
			 this.bannerad.sort(() => Math.random() - 0.5);
			 this._banner= this.bannerad[0];
	
 			this.jilishipingad.sort(() => Math.random() - 0.5);
			 this._shiping= this.jilishipingad[0];

	        this.chapingad.sort(() => Math.random() - 0.5);
			 this._chapingad= this.chapingad[0];
	         
             this.titletype=e.name;
             uni.setNavigationBarTitle({
                    title: e.name
                });
		 this.typeid=e.id,
			this.getData();
		}
		,onReady() {
		
				setTimeout(() => {
				this.type = "top"
		
            this.message = '终于等到您了，赶紧开启您的知识海洋吧！';
        this.$refs.popupMessage.open();
        
		let appbox = qq.createAppBox({
            adUnitId: '33c1913cfdb6ead1913ae84cf74d10c4'
        });
			}, 50000)
			
		},
		methods: {
            getData(){
					var _this=	this;
                uni.request({
					url: _this.URLs+"/api/question/getQuestion?tab=&name=" + this.typeid,
					 method: "post",
  					header: {"Content-Type": "text/plain; charset=utf-8"},
					dataType: 'text',
					data: {
						noncestr: Date.now()
					},
					success: (res) => {
							let data =JSON.parse( res.data);

		
					if(data.data.length>0){
						var da=data.data;
				
							for(var i = 0; i < da.length; i++){
						
							this.questionItem.push({answer:da[i].answer,question:da[i].question,q_shar:da[i].q_shar});
						}

						let qt	=this.questionItem[0];
							this.questionT=qt.question;
					
					}else{
						uni.showModal({
							content: "没有查到题库",
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
           	 				uni.navigateBack(); 
      				  } 
   						 }
						});
					
					}
				
					},
					fail: (err) => {
				
						uni.showModal({
							content: err.errMsg,
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
           	 				uni.navigateBack(); 
      				  } 
   						 }
						});
					
					},
					complete: () => {
						this.loading = false;
					}
				});
            },
          
            next(){
				this.current=this.current+1;
      this.questionItem.splice(0,1);
    if(this.questionItem.length>0){
          this.questionT=this.questionItem[0].question;
                                      
                                      }else
                                      {
												uni.showModal({
							content: "恭喜您完成今天任务！",
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
           	 				uni.navigateBack(); 
      				  } 
   						 }
						});
										}
            },
	
            looke(){
					this.current=this.current+1;
				let m=this;
				if(this.current>10){
                	this.jilishipingad.sort(() => Math.random() - 0.5);
                this. videoAd = qq.createRewardedVideoAd({adUnitId: this.jilishipingad[0]});
				this.videoAd.onError(function(res){  console.log('videoAd onError',res)});
				this.videoAd.onLoad(function(res){  console.log('videoAd onLoad',res)});
                this.videoAd.onClose(function(res){  
					if(res.isEnded){
                   
							uni.showModal({
							content:  m.questionItem[0].q_shar,
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
                                        m.questionItem.splice(0,1);
                                        if(m.questionItem.length>0){
                                            m.questionT= m.questionItem[0].question;
                                        }else{
									 								uni.showModal({
							content: "恭喜您完成今天任务！",
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
           	 				uni.navigateBack(); 
      				  } 
   						 }
						});
										}

                                     } 
   						 }
						});
					}else{
						m.type = "top";	 m.message = '必须看完才能进入下一题挑战！';	m.$refs.popupMessage.open();
					}
					console.log('videoAd onClose',res)});
				this.videoAd.load()  .then(() => {    console.log('激励视频加载成功');    
				this.videoAd.show().then(() => {      console.log('激励视频 广告显示成功')    }) 
				.catch(err => {      console.log('激励视频 广告显示失败')    })  })
				.catch(err => {    console.log('激励视频加载失败');  })
				}else{
					uni.showModal({
							content:  m.questionItem[0].q_shar,
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
                                        m.questionItem.splice(0,1);
                                        if(m.questionItem.length>0){
                                            m.questionT= m.questionItem[0].question;
                                        }else{
							uni.showModal({
							content: "恭喜您完成今天任务！",
							showCancel: false,
							 success: function (res) {
      							  if (res.confirm) {
           	 				uni.navigateBack(); 
      				  } 
   						 }
						});
										}

                                     } 
   						 }
						});
				}
            },

			/**
			 * 对话框取消按钮
			 */
			dialogClose(done) {
				// this.msgType = 'info'
				// this.message = '点击了对话框的取消按钮'
				// this.$refs.popupMessage.open()
				// // 需要执行 done 才能关闭对话框
				 done()
			},

		},
		}
	
</script>

<style>
	/* 头条小程序组件内不能引入字体 */
	/* #ifdef MP-TOUTIAO */
	@font-face {
		font-family: uniicons;
		font-weight: normal;
		font-style: normal;
		src: url('~@/static/uni.ttf') format('truetype');
	}
	.uni-list-cell {
		justify-content: flex-start
	}

	.word-btn-white {
		font-size: 18px;
		color: #FFFFFF;
	}

	.word-btn {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		border-radius: 6px;
		height: 48px;
		margin: 15px;
		background-color: #007AFF;
	}

	.word-btn--hover {
		background-color: #4ca2ff;
	}


	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 5px 15px;
	}


	.popup-content {
		background-color: #fff;
		padding: 15px;
	}

	/* #ifndef APP-NVUE */
	.button {
		margin: 0;
		padding: 0;
		line-height: 1;
	}

	/* #endif */

	.button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		flex: 1;
		height: 40px;
		margin: 0 15px;
	}

	.button-text {
		color: #fff;
		font-size: 14px;
	}

	.message {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding: 15px 0;
	}

	.dialog {
		padding: 15px 0;
	}

	.dialog-box {
		margin-left: 15px;
		margin-bottom: 15px;
	}

	.dialog-text {
		font-size: 14px;
		color: #666;
	}

	.share {
		padding: 15px 0;
	}

	.popup__success {
		color: #fff;
		background-color: #4cd964;
	}

	.popup__warn {
		color: #fff;
		background-color: #f0ad4e;
	}

	.popup__error {
		color: #fff;
		background-color: #dd524d;
	}

	.popup__info {
		color: #fff;
		background-color: #909399;
	}
</style>